package com.fhzn.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fhzn.auth.constant.Constants;
import com.fhzn.auth.entity.DataResource;
import com.fhzn.auth.enums.WebResponseEnum;
import com.fhzn.auth.mapper.DataResourceMapper;
import com.fhzn.auth.mapper.UserDataResourceMapper;
import com.fhzn.auth.service.DataResourceService;
import com.fhzn.auth.service.OperatorLogService;
import com.fhzn.auth.web.converter.DataResourceConverter;
import com.fhzn.auth.web.request.DataResourceOpRequest;
import com.fhzn.auth.web.vo.DataResourceVO;
import com.fhzn.commons.toolkit.exception.BuzException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author 老顾
 * @apiNote
 * @since 2024/1/19
 */
@Service
@RequiredArgsConstructor
public class DataResourceServiceImpl extends ServiceImpl<DataResourceMapper, DataResource> implements DataResourceService {

    private final DataResourceMapper dataResourceMapper;
    private final UserDataResourceMapper userDataResourceMapper;
    private final OperatorLogService operatorLogService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DataResourceVO add(DataResourceOpRequest request) {
        request.checkAdd();
        request.checkBaseParams();

        DataResource dataResource = request.addGenServiceParam();

        String parentCode = dataResource.getParentCode();
        if (StringUtils.isNotEmpty(parentCode)) {
            DataResource dataResourceInfo = dataResourceMapper.selectDataResourceByCode(dataResource.getAppCode(), parentCode);
            if (null == dataResourceInfo) {
                throw new BuzException("查找父资源出错 parentCode:" + parentCode);
            }
        }
        dataResourceMapper.insert(dataResource);
        operatorLogService.operatorLog(request.getHandler(), Constants.RESOURCE_SECTION, Constants.DATA_RESOURCE_ADD_METHOD, dataResource.getId(), null, dataResource, Constants.AUTH_APP_CODE);

        return DataResourceConverter.fromDataResource(dataResource);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteByCode(DataResourceOpRequest request) {
        request.checkBaseParams();

        DataResource dataResource = dataResourceMapper.selectDataResourceByTypeAndCode(request.getInvokeAppName(), request.getResourceType(), request.getCode());
        if (Objects.isNull(dataResource)) {
            throw new BuzException("数据资源不存在");
        }

        userDataResourceMapper.deleteByDataResourceId(dataResource.getId());
        dataResourceMapper.deleteById(dataResource);

        operatorLogService.operatorLog(request.getHandler(), Constants.RESOURCE_SECTION,
                Constants.DATA_RESOURCE_DELETE_METHOD,
                String.format("appcode:%s;resourceType:%s;code:%s", request.getInvokeAppName(), request.getResourceType(), request.getCode()),
                null, request.getId(), Constants.AUTH_APP_CODE);
        return WebResponseEnum.SUCCESS.getMessage();
    }

}
